package org.jeecg.modules.erp.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.erp.base.entity.ErpGoodsIngredientsReplace;
import org.jeecg.modules.erp.base.entity.ErpGoodsIngredientsReplaceInfo;
import org.jeecg.modules.erp.base.service.IErpGoodsIngredientsReplaceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Description: 原料代替品管理明细
* @Author: jeecg-boot
* @Date:   2024-05-24
* @Version: V1.0
*/
@Api(tags="原料代替品管理明细")
@RestController
@RequestMapping("/base/erpGoodsIngredientsReplaceInfo")
@Slf4j
public class ErpGoodsIngredientsReplaceInfoController {

   @Autowired
   private IErpGoodsIngredientsReplaceInfoService erpGoodsIngredientsReplaceInfoService;


    /**
     *
     * @param ingredientsId
     * @return
     */
   @ApiOperation(value="原料代替品管理-分页列表查询", notes="原料代替品管理-分页列表查询")
   @GetMapping(value = "/list")
   public Result<List<ErpGoodsIngredientsReplaceInfo>> list(String ingredientsId) {
       List<ErpGoodsIngredientsReplaceInfo> list = erpGoodsIngredientsReplaceInfoService.list(new LambdaQueryWrapper<ErpGoodsIngredientsReplaceInfo>()
               .eq(ErpGoodsIngredientsReplaceInfo::getIngredientsId, ingredientsId));
       return Result.OK(list);
   }

    /**
     *
     * @param erpGoodsIngredientsReplaceInfo
     * @return
     */
   @AutoLog(value = "原料代替品管理-添加")
   @ApiOperation(value="原料代替品管理-添加", notes="原料代替品管理-添加")
   //@RequiresPermissions("org.jeecg.modules.demo:erp_goods_ingredients_replace:add")
   @PostMapping(value = "/add")
   public Result<String> add(@RequestBody ErpGoodsIngredientsReplaceInfo erpGoodsIngredientsReplaceInfo) {
       ErpGoodsIngredientsReplace erpGoodsIngredientsReplace = new ErpGoodsIngredientsReplace();
//       BeanUtils.copyProperties(erpGoodsIngredientsReplacePage, erpGoodsIngredientsReplace);
//       erpGoodsIngredientsReplaceService.saveMain(erpGoodsIngredientsReplace, erpGoodsIngredientsReplacePage.getErpGoodsIngredientsReplaceInfoList());
       this.erpGoodsIngredientsReplaceInfoService.saveMain(erpGoodsIngredientsReplaceInfo);
       return Result.OK("添加成功！");
   }

    @AutoLog(value = "原料代替品管理-通过id删除")
    @ApiOperation(value="原料代替品管理-通过id删除", notes="原料代替品管理-通过id删除")
    //@RequiresPermissions("org.jeecg.modules.demo:erp_goods_ingredients_replace:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        this.erpGoodsIngredientsReplaceInfoService.removeById(id);
        return Result.OK("删除成功!");
    }


//
//   /**
//    *  编辑
//    *
//    * @param erpGoodsIngredientsReplacePage
//    * @return
//    */
//   @AutoLog(value = "原料代替品管理-编辑")
//   @ApiOperation(value="原料代替品管理-编辑", notes="原料代替品管理-编辑")
//   //@RequiresPermissions("org.jeecg.modules.demo:erp_goods_ingredients_replace:edit")
//   @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
//   public Result<String> edit(@RequestBody ErpGoodsIngredientsReplacePage erpGoodsIngredientsReplacePage) {
//       ErpGoodsIngredientsReplace erpGoodsIngredientsReplace = new ErpGoodsIngredientsReplace();
//       BeanUtils.copyProperties(erpGoodsIngredientsReplacePage, erpGoodsIngredientsReplace);
//       ErpGoodsIngredientsReplace erpGoodsIngredientsReplaceEntity = erpGoodsIngredientsReplaceService.getById(erpGoodsIngredientsReplace.getId());
//       if(erpGoodsIngredientsReplaceEntity==null) {
//           return Result.error("未找到对应数据");
//       }
//       erpGoodsIngredientsReplaceService.updateMain(erpGoodsIngredientsReplace, erpGoodsIngredientsReplacePage.getErpGoodsIngredientsReplaceInfoList());
//       return Result.OK("编辑成功!");
//   }
//
//   /**
//    *   通过id删除
//    *
//    * @param id
//    * @return
//    */

//
//   /**
//    *  批量删除
//    *
//    * @param ids
//    * @return
//    */
//   @AutoLog(value = "原料代替品管理-批量删除")
//   @ApiOperation(value="原料代替品管理-批量删除", notes="原料代替品管理-批量删除")
//   //@RequiresPermissions("org.jeecg.modules.demo:erp_goods_ingredients_replace:deleteBatch")
//   @DeleteMapping(value = "/deleteBatch")
//   public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//       this.erpGoodsIngredientsReplaceService.delBatchMain(Arrays.asList(ids.split(",")));
//       return Result.OK("批量删除成功！");
//   }
//
//   /**
//    * 通过id查询
//    *
//    * @param id
//    * @return
//    */
//   //@AutoLog(value = "原料代替品管理-通过id查询")
//   @ApiOperation(value="原料代替品管理-通过id查询", notes="原料代替品管理-通过id查询")
//   @GetMapping(value = "/queryById")
//   public Result<ErpGoodsIngredientsReplace> queryById(@RequestParam(name="id",required=true) String id) {
//       ErpGoodsIngredientsReplace erpGoodsIngredientsReplace = erpGoodsIngredientsReplaceService.getById(id);
//       if(erpGoodsIngredientsReplace==null) {
//           return Result.error("未找到对应数据");
//       }
//       return Result.OK(erpGoodsIngredientsReplace);
//
//   }
//
//   /**
//    * 通过id查询
//    *
//    * @param id
//    * @return
//    */
//   //@AutoLog(value = "原料替代品管理明细表通过主表ID查询")
//   @ApiOperation(value="原料替代品管理明细表主表ID查询", notes="原料替代品管理明细表-通主表ID查询")
//   @GetMapping(value = "/queryErpGoodsIngredientsReplaceInfoByMainId")
//   public Result<List<ErpGoodsIngredientsReplaceInfo>> queryErpGoodsIngredientsReplaceInfoListByMainId(@RequestParam(name="id",required=true) String id) {
//       List<ErpGoodsIngredientsReplaceInfo> erpGoodsIngredientsReplaceInfoList = erpGoodsIngredientsReplaceInfoService.selectByMainId(id);
//       return Result.OK(erpGoodsIngredientsReplaceInfoList);
//   }
//
//   /**
//   * 导出excel
//   *
//   * @param request
//   * @param erpGoodsIngredientsReplace
//   */
//   //@RequiresPermissions("org.jeecg.modules.demo:erp_goods_ingredients_replace:exportXls")
//   @RequestMapping(value = "/exportXls")
//   public ModelAndView exportXls(HttpServletRequest request, ErpGoodsIngredientsReplace erpGoodsIngredientsReplace) {
//     // Step.1 组装查询条件查询数据
//     QueryWrapper<ErpGoodsIngredientsReplace> queryWrapper = QueryGenerator.initQueryWrapper(erpGoodsIngredientsReplace, request.getParameterMap());
//     LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//
//     //配置选中数据查询条件
//     String selections = request.getParameter("selections");
//     if(oConvertUtils.isNotEmpty(selections)) {
//        List<String> selectionList = Arrays.asList(selections.split(","));
//        queryWrapper.in("id",selectionList);
//     }
//     //Step.2 获取导出数据
//     List<ErpGoodsIngredientsReplace> erpGoodsIngredientsReplaceList = erpGoodsIngredientsReplaceService.list(queryWrapper);
//
//     // Step.3 组装pageList
//     List<ErpGoodsIngredientsReplacePage> pageList = new ArrayList<ErpGoodsIngredientsReplacePage>();
//     for (ErpGoodsIngredientsReplace main : erpGoodsIngredientsReplaceList) {
//         ErpGoodsIngredientsReplacePage vo = new ErpGoodsIngredientsReplacePage();
//         BeanUtils.copyProperties(main, vo);
//         List<ErpGoodsIngredientsReplaceInfo> erpGoodsIngredientsReplaceInfoList = erpGoodsIngredientsReplaceInfoService.selectByMainId(main.getId());
//         vo.setErpGoodsIngredientsReplaceInfoList(erpGoodsIngredientsReplaceInfoList);
//         pageList.add(vo);
//     }
//
//     // Step.4 AutoPoi 导出Excel
//     ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
//     mv.addObject(NormalExcelConstants.FILE_NAME, "原料代替品管理列表");
//     mv.addObject(NormalExcelConstants.CLASS, ErpGoodsIngredientsReplacePage.class);
//     mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("原料代替品管理数据", "导出人:"+sysUser.getRealname(), "原料代替品管理"));
//     mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
//     return mv;
//   }
//
//   /**
//   * 通过excel导入数据
//   *
//   * @param request
//   * @param response
//   * @return
//   */
//   //@RequiresPermissions("org.jeecg.modules.demo:erp_goods_ingredients_replace:importExcel")
//   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//     MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//     Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
//     for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
//         // 获取上传文件对象
//         MultipartFile file = entity.getValue();
//         ImportParams params = new ImportParams();
//         params.setTitleRows(2);
//         params.setHeadRows(1);
//         params.setNeedSave(true);
//         try {
//             List<ErpGoodsIngredientsReplacePage> list = ExcelImportUtil.importExcel(file.getInputStream(), ErpGoodsIngredientsReplacePage.class, params);
//             for (ErpGoodsIngredientsReplacePage page : list) {
//                 ErpGoodsIngredientsReplace po = new ErpGoodsIngredientsReplace();
//                 BeanUtils.copyProperties(page, po);
//                 erpGoodsIngredientsReplaceService.saveMain(po, page.getErpGoodsIngredientsReplaceInfoList());
//             }
//             return Result.OK("文件导入成功！数据行数:" + list.size());
//         } catch (Exception e) {
//             log.error(e.getMessage(),e);
//             return Result.error("文件导入失败:"+e.getMessage());
//         } finally {
//             try {
//                 file.getInputStream().close();
//             } catch (IOException e) {
//                 e.printStackTrace();
//             }
//         }
//     }
//     return Result.OK("文件导入失败！");
//   }

}
